package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.pojo.CheckGroup;
import com.itheima.common.pojo.CheckItem;
import com.itheima.common.pojo.dto.CheckGroupPageQueryDTO;
import com.itheima.mapper.CheckGroupCheckItemMapper;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;
    @Autowired
    private CheckGroupCheckItemMapper checkGroupCheckItemMapper;

    /**
     * 新增检查组
     * @param checkGroup
     */
    @Transactional
    public void saveWithCheckItems(CheckGroup checkGroup) {
        //向检查组表中插入一条数据
        checkGroupMapper.insert(checkGroup);
        //获取insert语句生成的主键值
        Integer checkGroupId = checkGroup.getId();
        //获取检查组和检查项的关联关系集合
        List<CheckItem> checkItems = checkGroup.getCheckItems();
        if (checkItems != null && !checkItems.isEmpty()) {
            for (CheckItem checkItem : checkItems) {
                checkItem.setCheckGroupId(checkGroupId);
            }
            checkGroupCheckItemMapper.insertBatch(checkItems);
        }
    }

    /**
     * 删除检查组
     * @param id
     */
    @Transactional
    public void deleteById(Integer id) {
        //删除检查组
        checkGroupMapper.deleteById(id);
        //删除检查组和检查项的关联关系
        checkItemMapper.deleteByCheckGroupId(id);
    }

    /**
     * 修改检查组及其检查项信息
     * @param checkGroup
     */
    @Transactional
    public void update(CheckGroup checkGroup) {
        //修改检查组基本信息
        checkGroupMapper.update(checkGroup);
        //修改检查组的检查项信息
        //删除原有的检查项
        checkItemMapper.deleteByCheckGroupId(checkGroup.getId());
        //添加新的检查项信息
        List<CheckItem> checkItems = checkGroup.getCheckItems();
        if (checkItems != null && !checkItems.isEmpty()) {
            checkGroupCheckItemMapper.insertBatch(checkItems);
        }
    }

    /**
     * 根据项目编码/项目名称分页查询检查组
     * @param checkGroupDTO
     * @return
     */
    public PageResult pageQuery(CheckGroupPageQueryDTO checkGroupDTO) {
        PageHelper.startPage(checkGroupDTO.getCurrentPage(), checkGroupDTO.getPageSize());
        Page<CheckGroup> page = checkGroupMapper.pageQuery(checkGroupDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据检查项目名称和检查项目编码查询检查项列表
     * @param code
     * @param name
     * @return
     */
    public List<CheckGroup> findAll(String code, String name) {
        return checkGroupMapper.findAll(code, name);
    }

    /**
     * 根据套餐id查询检查组id
     * @param setmealId
     * @return
     */
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {
        return checkGroupMapper.findCheckGroupIdsBySetmealId(setmealId);
    }

    /**
     * 根据检查组id查询检查组信息
     * @param id
     * @return
     */
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.findById(id);
    }
}
